package com.dhsc.service.impl.jyjk.zxyc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhsc.domain.jkzx.zxjh.GlxxVo;
import com.dhsc.domain.jkzx.zxjh.KhglgrxxVo;
import com.dhsc.domain.jkzx.zxjh.KhgljgxxVo;
import com.dhsc.domain.jkzx.zxjh.KhglxxVo;
import com.dhsc.domain.jkzx.zxjh.KhglzcxxVo;
import com.dhsc.domain.jkzx.zxjh.KhxxVo;
import com.dhsc.domain.jkzx.zxjh.TJjrycVo;
import com.dhsc.domain.jkzx.zxjh.WtxxVo;
import com.dhsc.domain.khzl.common.CommonVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.domain.yypt.yhzx.TJgxxVo;
import com.dhsc.domain.yypt.yhzx.TYhxxVo;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.yypt.IYhzxDao;
import com.dhsc.service.jyjk.zxyc.ITJjrJkService;
import com.dhsc.util.CommonUtil;
import com.dhsc.util.IDCard;
import com.dhsc.view.dao.jyjk.zxyc.ITJjrJkDao;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TJjrJkServiceImpl implements ITJjrJkService{
	
	@Autowired
	private ITJjrJkDao jjrJkDao;

	@Autowired
	private IYhzxDao yhzxDao;
	
	@Override
	public LayuiTableVo queryTJjryc(TJjrycVo t) {
		LayuiTableVo obj = new LayuiTableVo();
		try{
			if(!CommonUtil.isNotEmptyObj(t.getZt())){
				PageMethod.startPage(t.getPage(), t.getLimit());
			}
			List<TJjrycVo> list = jjrJkDao.queryTJjryc(t);
			PageInfo<TJjrycVo> pageInfo = new PageInfo<>(list);
			if(CommonUtil.isNotEmptyList(list))
			{
				List<TYhxxVo> yhlist = yhzxDao.queryTYhxx(new TYhxxVo());
				List<TJgxxVo> jglist = yhzxDao.queryTJgxx(new TJgxxVo());
				for(int i = 0;i < list.size();i ++)
				{
					for(int j = 0;j < yhlist.size();j ++)
					{
						if(CommonUtil.isNotEmptyObj(list.get(i).getClr()) 
								&& list.get(i).getClr().equals(String.valueOf(yhlist.get(j).getUserid()))){
							list.get(i).setClrmc(yhlist.get(j).getTruename());
						}
					}
					for(int j = 0;j < jglist.size();j ++)
					{
						if(CommonUtil.isNotEmptyObj(list.get(i).getYyb()) 
								&& list.get(i).getYyb().equals(jglist.get(j).getJgdm())){
							list.get(i).setYybmc(jglist.get(j).getBmmc());
						}
					}
				}
			}
			obj.setCount(pageInfo.getTotal());
			obj.setData(list);
			obj.setMsg("");
			obj.setCode(0);
		}catch(Exception e){
			log.error("查询经纪人异常信息出错", e);
			throw new MyException("查询经纪人异常信息出错",e);
		}
		return obj;
	}

	@Override
	public ResponseObject selectJklx(TJjrycVo t) {
		ResponseObject obj = new ResponseObject();
		try{
			List<CommonVo> list = jjrJkDao.selectJklx(t);
			obj.setData(list);
			obj.setStatus(200);
		}catch(Exception e){
			log.error("查询经纪人监控类型出错", e);
			throw new MyException("查询经纪人监控类型出错",e);
		}
		return obj;
	}

	@Override
	public ResponseObject selectJjrYyb(TJjrycVo t) {
		ResponseObject obj = new ResponseObject();
		try{
			List<CommonVo> list = jjrJkDao.selectJjrYyb(t);
			if(CommonUtil.isNotEmptyList(list))
			{
				List<TJgxxVo> jglist = yhzxDao.queryTJgxx(new TJgxxVo());
				for(int i = 0;i < list.size();i ++)
				{
					for(int j = 0;j < jglist.size();j ++)
					{
						if(list.get(i).getId().equals(jglist.get(j).getJgdm()))
						{
							list.get(i).setMc(jglist.get(j).getBmmc());
						}
					}
				}
			}
			obj.setData(list);
			obj.setStatus(200);
		}catch(Exception e){
			log.error("查询经纪人营业部出错", e);
			throw new MyException("查询经纪人营业部出错",e);
		}
		return obj;
	}

	@Override
	public LayuiTableVo querykhxx(KhxxVo k) {
		LayuiTableVo obj = new LayuiTableVo();
		try{
			String zjlb = jjrJkDao.selectZjlbByZjbh(k);
			String zjbh = k.getZjbh();
			if(CommonUtil.isNotEmptyObj(zjbh))
			{
				if(CommonUtil.isNotEmptyObj(zjlb))
				{
					if(zjlb.equals("0")&&zjbh.length()==15)
					{
						zjbh = zjbh + "," + IDCard.get18Ic(zjbh);
						k.setZjbhs(zjbh.split(","));
					}else if(zjlb.equals("0")&&zjbh.length()==18)
					{
						zjbh = zjbh + "," + IDCard.get15Ic(zjbh);
						k.setZjbhs(zjbh.split(","));
					}else{
						k.setZjbhs(zjbh.split(","));
					}
				}else{
					k.setZjbhs(zjbh.split(","));
				}
			}else{
				k.setZjbh("");
			}
			List<KhxxVo> list = jjrJkDao.querykhxx(k);
			obj.setData(list);
			obj.setMsg("");
			obj.setCount(list.size());
			obj.setCode(0);
		}catch(Exception e){
			log.error("查询经纪人异常页面客户信息异常", e);
			throw new MyException("查询经纪人异常页面客户信息异常",e);
		}
		return obj;
	}

	@Override
	public ResponseObject selectMonitorInfo(KhxxVo k) {
		ResponseObject obj = new ResponseObject();
		try{
			String zjlb = jjrJkDao.selectZjlbByZjbh(k);
			String zjbh = k.getZjbh();
			if(CommonUtil.isNotEmptyObj(zjbh))
			{
				if(CommonUtil.isNotEmptyObj(zjlb))
				{
					if(zjlb.equals("0")&&zjbh.length()==15)
					{
						zjbh = zjbh + "," + IDCard.get18Ic(zjbh);
						k.setZjbhs(zjbh.split(","));
					}else if(zjlb.equals("0")&&zjbh.length()==18)
					{
						zjbh = zjbh + "," + IDCard.get15Ic(zjbh);
						k.setZjbhs(zjbh.split(","));
					}else{
						k.setZjbhs(zjbh.split(","));
					}
				}else{
					k.setZjbhs(zjbh.split(","));
				}
			}else{
				k.setZjbh("");
			}
			List<KhxxVo> list = jjrJkDao.selectMonitorInfo(k);
			obj.setData(list);
			obj.setStatus(200);
		}catch(Exception e){
			log.error("查询异常页面监控信息异常", e);
			throw new MyException("查询异常页面监控信息异常",e);
		}
		return obj;
	}

	@Override
	public ResponseObject queryzssjyurl(KhxxVo k) {
		ResponseObject obj = new ResponseObject();
		try{
			List<KhxxVo> list = jjrJkDao.queryzssjyurl(k);
			obj.setData(list);
			obj.setStatus(200);
		}catch(Exception e){
			log.error("查询自动补全信息异常", e);
			throw new MyException("查询自动补全信息异常",e);
		}
		return obj;
	}

	@Override
	public LayuiTableVo selectMonitorInfoJJR(KhxxVo k) {
		LayuiTableVo obj = new LayuiTableVo();
		try{
			String zjlb = jjrJkDao.selectZjlbByZjbh(k);
			String zjbh = k.getZjbh();
			if(CommonUtil.isNotEmptyObj(zjbh))
			{
				if(CommonUtil.isNotEmptyObj(zjlb))
				{
					if(zjlb.equals("0")&&zjbh.length()==15)
					{
						zjbh = zjbh + "," + IDCard.get18Ic(zjbh);
						k.setZjbhs(zjbh.split(","));
					}else if(zjlb.equals("0")&&zjbh.length()==18)
					{
						zjbh = zjbh + "," + IDCard.get15Ic(zjbh);
						k.setZjbhs(zjbh.split(","));
					}else{
						k.setZjbhs(zjbh.split(","));
					}
				}else{
					k.setZjbhs(zjbh.split(","));
				}
			}else{
				k.setZjbh("");
			}
			TJjrycVo jjrycVo = new TJjrycVo();
			jjrycVo.setKhh(k.getKhh());
			jjrycVo.setZjbhs(k.getZjbhs());
			jjrycVo.setPage(k.getPage());
			jjrycVo.setLimit(k.getLimit());
			jjrycVo.setYyb(k.getYyb());
			PageMethod.startPage(jjrycVo.getPage(), jjrycVo.getLimit());
			List<TJjrycVo> list = jjrJkDao.queryTJjryc(jjrycVo);
			PageInfo<TJjrycVo> pageInfo = new PageInfo<>(list);
			obj.setData(list);
			obj.setMsg("");
			obj.setCount(pageInfo.getTotal());
			obj.setCode(0);
		}catch(Exception e){
			log.error("查询经纪人异常页面监控信息异常", e);
			throw new MyException("查询经纪人异常页面监控信息异常",e);
		}
		return obj;
	}

	@Override
	public ResponseObject insertzssjyurl(KhxxVo k) {
		ResponseObject obj = new ResponseObject();
		try {
			String zjlb = jjrJkDao.selectZjlbByZjbh(k);
			String zjbh = k.getZjbh();
			if(CommonUtil.isNotEmptyObj(zjbh))
			{
				if(CommonUtil.isNotEmptyObj(zjlb))
				{
					if(zjlb.equals("0") && zjbh.length() == 15)
					{
						zjbh = zjbh + "," + IDCard.get18Ic(zjbh);
						k.setZjbhs(zjbh.split(","));
					}else if(zjlb.equals("0") && zjbh.length() == 18)
					{
						zjbh = zjbh + "," + IDCard.get15Ic(zjbh);
						k.setZjbhs(zjbh.split(","));
					}else{
						k.setZjbhs(zjbh.split(","));
					}
				}else{
					k.setZjbhs(zjbh.split(","));
				}
			}else{
				k.setZjbh("");
			}
			TJjrycVo jjrycVo = new TJjrycVo();
			jjrycVo.setZjbhs(k.getZjbhs());
			List<TJjrycVo> list1 = jjrJkDao.queryTJjryc(jjrycVo);
			if(CommonUtil.isNotEmptyList(list1)){
				List<KhxxVo> list = jjrJkDao.queryzssjyurl(k);
				//如果查询不到证件编号则新增
				if (!CommonUtil.isNotEmptyList(list))
				{
					 jjrJkDao.insertzssjy(k);
				}
			}
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("经纪人页面保存查询条件异常", e);
			throw new MyException("经纪人页面保存查询条件异常",e);
		}
		return obj;
	}

	@Override
	public ResponseObject insertzssjykhhurl(KhxxVo k) {
		ResponseObject obj = new ResponseObject();
		try {
			List<KhxxVo> list = jjrJkDao.querykhxx(k);
			if(CommonUtil.isNotEmptyList(list)){
				List<KhxxVo> list1 = jjrJkDao.queryzssjyurl(k);
				//如果查询不到证件编号则新增
				if (!CommonUtil.isNotEmptyList(list1))
				{
					jjrJkDao.insertzssjy(k);
				}
			}
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("经纪人页面保存查询条件异常", e);
			throw new MyException("经纪人页面保存查询条件异常",e);
		}
		return obj;
	}

	@Override
	public ResponseObject selectAllOfCustomer(KhglxxVo k) {
		ResponseObject obj = new ResponseObject();
		try {
			KhglxxVo khglxxVo = jjrJkDao.selectAllOfCustomer(k);
			if(CommonUtil.isNotEmptyObj(khglxxVo))
			{
				List<TJgxxVo> jglist = yhzxDao.queryTJgxx(new TJgxxVo());
				for(int j = 0;j < jglist.size();j ++)
				{
					if(khglxxVo.getYyb().equals(jglist.get(j).getJgdm()))
					{
						khglxxVo.setYyb(jglist.get(j).getBmmc());
					}
				}
			}
			obj.setData(khglxxVo);
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("查询客户号关联的全部信息客户信息异常", e);
			throw new MyException("查询客户号关联的全部信息客户信息异常",e);
		}
		return obj;
	}

	@Override
	public ResponseObject selectAllOfAsset(KhglzcxxVo k) {
		ResponseObject obj = new ResponseObject();
		try {
			KhglzcxxVo khglzcxxVo = jjrJkDao.selectAllOfAsset(k);
			obj.setData(khglzcxxVo);
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("查询客户号关联的全部信息资产信息异常", e);
			throw new MyException("查询客户号关联的全部信息资产信息异常",e);
		}
		return obj;
	}

	@Override
	public LayuiTableVo selectTzqgl(KhglzcxxVo k) {
		LayuiTableVo obj = new LayuiTableVo();
		try {
			PageMethod.startPage(k.getPage(), 10);
			List<KhglzcxxVo> list = jjrJkDao.selectTzqgl(k);
			PageInfo<KhglzcxxVo> pageInfo = new PageInfo<>(list);
			obj.setCount(pageInfo.getTotal());
			obj.setData(list);
			obj.setMsg("");
			obj.setCode(0);
		} catch (Exception e) {
			log.error("查询客户关联信息资产信息普通账户持仓异常", e);
			throw new MyException("查询客户关联信息资产信息普通账户持仓异常",e);
		}
		return obj;
	}

	@Override
	public LayuiTableVo selectTzqglRzrq(KhglzcxxVo k) {
		LayuiTableVo obj = new LayuiTableVo();
		try {
			PageMethod.startPage(k.getPage(), 10);
			List<KhglzcxxVo> list = jjrJkDao.selectTzqglRzrq(k);
			PageInfo<KhglzcxxVo> pageInfo = new PageInfo<>(list);
			obj.setCount(pageInfo.getTotal());
			obj.setData(list);
			obj.setMsg("");
			obj.setCode(0);
		} catch (Exception e) {
			log.error("查询客户关联信息资产信息信用账户持仓异常", e);
			throw new MyException("查询客户关联信息资产信息信用账户持仓异常",e);
		}
		return obj;
	}

	@Override
	public LayuiTableVo selectAllOfTradeinfoPT(WtxxVo w) {
		LayuiTableVo obj = new LayuiTableVo();
		try {
			PageMethod.startPage(w.getPage(), w.getLimit());
			List<WtxxVo> list = jjrJkDao.selectAllOfTradeinfoPT(w);
			PageInfo<WtxxVo> pageInfo = new PageInfo<>(list);
			obj.setCount(pageInfo.getTotal());
			obj.setData(list);
			obj.setMsg("");
			obj.setCode(0);
		} catch (Exception e) {
			log.error("查询根据客户号委托日期和异常小类查询明细详情异常", e);
			throw new MyException("查询根据客户号委托日期和异常小类查询明细详情异常",e);
		}
		return obj;
	}

	@Override
	public LayuiTableVo selectAllOfTradeinfoXY(WtxxVo w) {
		LayuiTableVo obj = new LayuiTableVo();
		try {
			PageMethod.startPage(w.getPage(), w.getLimit());
			List<WtxxVo> list = jjrJkDao.selectAllOfTradeinfoXY(w);
			PageInfo<WtxxVo> pageInfo = new PageInfo<>(list);
			obj.setCount(pageInfo.getTotal());
			obj.setData(list);
			obj.setMsg("");
			obj.setCode(0);
		} catch (Exception e) {
			log.error("查询客户号关联的全部信息交易信息信用账户交易异常", e);
			throw new MyException("查询客户号关联的全部信息交易信息信用账户交易异常",e);
		}
		return obj;
	}

	@Override
	public ResponseObject selectAllOfPersonal(KhglgrxxVo k) {
		ResponseObject obj = new ResponseObject();
		try {
			KhglgrxxVo khglgrxxVo = jjrJkDao.selectAllOfPersonal(k);
			obj.setData(khglgrxxVo);
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("查询客户号关联的全部信息个人详细信息异常", e);
			throw new MyException("查询客户号关联的全部信息个人详细信息异常",e);
		}
		return obj;
	}

	@Override
	public ResponseObject selectAllOfOrgans(KhgljgxxVo k) {
		ResponseObject obj = new ResponseObject();
		try {
			KhgljgxxVo khgljgxxVo = jjrJkDao.selectAllOfOrgans(k);
			obj.setData(khgljgxxVo);
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("查询客户号关联的全部信息机构详细信息异常", e);
			throw new MyException("查询客户号关联的全部信息机构详细信息异常",e);
		}
		return obj;
	}

	@Override
	public LayuiTableVo queryGlxxInfo(GlxxVo g) {
		LayuiTableVo obj = new LayuiTableVo();
		try {
			if(g.getCzzd() == null && g.getWtrq() == null){
				obj.setData(null);
			}else{
				PageMethod.startPage(g.getPage(), g.getLimit());
				List<GlxxVo> list = jjrJkDao.queryGlxxInfo(g);
				PageInfo<GlxxVo> pageInfo = new PageInfo<>(list);
				obj.setData(list);
				obj.setCount(pageInfo.getTotal());
			}
			obj.setMsg("");
			obj.setCode(0);
		} catch (Exception e) {
			log.error("查询关联信息出错", e);
			throw new MyException("查询关联信息出错",e);
		}
		return obj;
	}

}
